<?php

declare(strict_types=1);

namespace App\CustHub\Controller;

use App\CustHub\Annotation\ApiAuth;
use App\CustHub\Annotation\ChOperationLog;
use App\CustHub\Annotation\ChPermission;
use App\CustHub\Annotation\VerifySignature;
use App\CustHub\Request\SmartAutoPushRequest;
use App\CustHub\Service\ChSmartAutoPushService;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\PostMapping;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Psr\Http\Message\ResponseInterface;

/**
 * 智能运营-自动推送
 */
#[Controller(prefix: "ch/autoPush"), ApiAuth, VerifySignature]
class SmartAutoPushController extends BaseController
{
    #[Inject]
    protected ChSmartAutoPushService $service;


    #[PostMapping('list'), ChPermission('ch:smart:auto:push:list')]
    public function list(SmartAutoPushRequest $request): ResponseInterface
    {
        return $this->success($this->service->getAutoPushList($request->all()));
    }

    /**
     * 新增方案
     * @param SmartAutoPushRequest $request
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    #[PostMapping('save'), ChPermission('ch:smart:auto:push:save'),ChOperationLog]
    public function save(SmartAutoPushRequest $request): ResponseInterface
    {
        return $this->success('添加成功', ['id' => $this->service->save($request->all())]);
    }

    /**
     * 更新方案
     * @param SmartAutoPushRequest $request
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    #[PostMapping('update'), ChPermission('ch:smart:auto:push:update'),ChOperationLog]
    public function update(SmartAutoPushRequest $request): ResponseInterface
    {
        $rst = $this->service->update($request->input('id'),$request->all());
        return $rst ? $this->success('编辑成功') : $this->error('编辑失败');
    }


    /**
     * 删除方案
     * @param SmartAutoPushRequest $request
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws \Exception
     */
    #[PostMapping('delete'), ChPermission('ch:smart:auto:push:update'),ChOperationLog]
    public function delete(SmartAutoPushRequest $request): ResponseInterface
    {
        $rst = $this->service->deleteAutoPush($request->all());
        return $rst ? $this->success('删除成功') : $this->error('删除失败');
    }
}